﻿@using M2B.GestionEntretiens.Helpers.HTML
@using M2B.GestionEntretiens.POCOS
@model M2B.GestionEntretiens.Models.CandidatsModel

@{
    ViewBag.Title = "Entretiens";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script type="text/javascript">

    // function to return a JSON object form a JSON.NET serialized object with $id/$ref key-values
    // obj: the obj of interest.
    // parentObj: the top level object containing all child objects as serialized by JSON.NET.
    function getJsonNetObject(obj, parentObj) {
        // check if obj has $id key.
        var objId = obj["$id"];
        if (typeof (objId) !== "undefined" && objId != null) {
            // $id key exists, so you have the actual object... return it
            return obj;
        }
        // $id did not exist, so check if $ref key exists.
        objId = obj["$ref"];
        if (typeof (objId) !== "undefined" && objId != null) {
            // $ref exists, we need to get the actual object by searching the parent object for $id
            return getJsonNetObjectById(parentObj, objId);
        }
        // $id and $ref did not exist... return null
        return null;
    }

    // function to return a JSON object by $id
    // parentObj: the top level object containing all child objects as serialized by JSON.NET.
    // id: the $id value of interest
    function getJsonNetObjectById(parentObj, id) {
        // check if $id key exists.
        var objId = parentObj["$id"];
        if (typeof (objId) !== "undefined" && objId != null && objId == id) {
            // $id key exists, and the id matches the id of interest, so you have the object... return it
            return parentObj;
        }
        for (var i in parentObj) {
            if (typeof (parentObj[i]) == "object" && parentObj[i] != null) {
                //going one step down in the object tree
                var result = getJsonNetObjectById(parentObj[i], id);
                if (result != null) {
                    // return found object
                    return result;
                }
            }
        }
        return null;
    }

</script>




<script type="text/javascript">
    var statutsEntretiens = JSON.parse('@Html.Raw(Json.Encode((List<Referentiel>)ViewData["StatutsEntretiens"]))');
    var langueEntretiens = JSON.parse('@Html.Raw(Json.Encode((List<Referentiel>)ViewData["LangueEntretiens"]))');

</script>

<script type="text/javascript">
    var entretienId = 0;
    //La liste des entertiens
    var entretiens;
    $(document).ready(function () {

        LoadEntretiens();

    });
    //Chargement des candidats
    function LoadEntretiens() {

        $.ajax({
            url: '@Url.Action("LoadEntretiens","Entretiens")',
            type: 'GET',
            cache: false,
            success: function (result) {


                entretiens = JSON.parse(result);

                LoadGridEntretiens();


            }
        });

    }

    function LoadGridEntretiens() {

        jQuery("#EntretiensTable").jqGrid({
            datatype: "jsonstring",
            datastr: entretiens,
            colNames: ["<img style='height:16px;width:16px;' src='../Content/Images/Ajouter.png' onclick='AjouterEntretien()'>", 'Id', 'Titre', 'Date entretien', 'Candidat', 'Evaluation', 'Langue', 'Statut', 'Commentaire'],
            colModel: [
                { name: 'act', index: 'act', width: 65, sortable: false, search: false },
                { name: 'EntretienId', index: 'EntretienId', width: 40, sorttype: "int", key: true, hidden: true },
                { name: 'Titre', index: 'Titre', width: 200, sorttype: "string" },
                {
                    name: 'DateEntretien', index: 'DateEntretien', width: 80, align: "right", sorttype: "date", formatter: 'date',
                    formatoptions: {
                        srcformat: 'ISO8601Long',
                        newformat: 'd/m/Y',
                        defaultValue: null
                    }
                },
                {
                    name: 'Candidat',
                    index: 'Candidat',
                    width: 200,
                    sorttype: "integer", align: "right",
                    formatter: function (Candidat) {

                        var item = getJsonNetObject(Candidat, entretiens);
                        var result = item.Prenom + " " + item.Nom;

                        return result;
                    }
                },
                {
                    name: 'Evaluation',
                    index: 'Evaluation',
                    width: 100,
                    sorttype: "integer", align: "right",
                    formatter: function (Evaluation) {
                        var result = "";
                        for (var i = 0; i < Evaluation; i++) {
                            result += "<img style='height:16px;width:16px;' src='../Content/Images/Rating.png'/>";
                        }
                        return result;
                    }
                },
                {
                    name: 'Langue',
                    index: 'Langue',
                    width: 100,
                    sorttype: "string",
                    align: "left",
                    formatter: function (Langue) {

                        var result = "";
                        for (var i = 0; i < langueEntretiens.length; i++) {

                            if (langueEntretiens[i].Code == Langue) {
                                result = langueEntretiens[i].Valeur;
                                break;
                            }
                        }

                        return result;
                    }
                },
                {
                    name: 'Statut',
                    index: 'Statut',
                    width: 200,
                    sorttype: "string",
                    align: "left",
                    formatter: function (Statut) {

                        var result = "";
                        for (var i = 0; i < statutsEntretiens.length; i++) {
                          
                            if (statutsEntretiens[i].Code == Statut) {
                                result = statutsEntretiens[i].Valeur;
                                break;
                            }
                        }

                        return result;
                    }
                },
                { name: 'Commentaire', index: 'Commentaire', width: 200, sorttype: "string", align: "left", }

            ],
            gridComplete: function () {
                var ids = jQuery("#EntretiensTable").jqGrid('getDataIDs');
                for (var i = 0; i < ids.length; i++) {
                    var data = ids[i];
                    var btnEdit = "<img style='height:16px;width:16px;margin-right:5px;' src='../Content/Images/Editer.png' onclick='EditEntretien(" + data + ")' />";
                    var btnSupprimer = "<img style='height:16px;width:16px;' src='../Content/Images/Supprimer.png' onclick='AfficherDeleteEntretienPopUp(" + data + ")' />";
                    jQuery("#EntretiensTable").jqGrid('setRowData', ids[i], { act: btnEdit + btnSupprimer });
                }
            },
            rowNum: 20,
            rowList: [10, 20, 30],
            pager: '#EntretiensPager',
            height: 480,
            sortname: 'Titre',
            width: 950,
            viewrecords: true,
            loadonce: true,
            sortorder: "asc",
            multiselect: false,
            caption: "Entretiens"


        });



        jQuery("#EntretiensTable").jqGrid('navGrid', '#EntretiensPager', { edit: false, add: false, save: false, del: false });

        jQuery("#EntretiensTable").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false, defaultSearch: "cn" });

    }

</script>
@*La liste des méthodes*@
<script type="text/javascript">

    function EditEntretien(Id) {

        entretienId = Id;
        $.ajax({
            type: "GET",
            data: { entretienId: entretienId },
            url: '@Url.Action("EditEntretien")',
            success: function (result) {

                $("#dialogEditEntretien").html(result).dialog({ width: 800, position: ['center', 'top'], modal: true });


            }
        });
    }

    function AfficherDeleteEntretienPopUp(Id) {

        entretienId = Id;
       
        $("#EntretiensTable").setSelection(entretienId, false);
        $("#dialogDeleteEntretien").dialog({ width: 400, position: ['center', 'center'], modal: true });
    }

    function DeleteEntretien() {

        $.ajax({
            url: '@Url.Action("DeleteEntretien")',
            type: 'POST',
            data: { entretienId: entretienId }, success: function () {
                $('#EntretiensTable').delRowData(entretienId);
                $("#dialogDeleteEntretien").dialog("close");
            }
        });

    }

    function AjouterEntretien() {

        $.ajax({
            type: "GET",
            url: '@Url.Action("AddEntretien")',
            success: function (result) {

                $("#dialogAddEntretien").html(result).dialog({ width: 800, position: ['center', 'top'], modal: true, draggable: false });


            }
        });
    }
</script>



<table id="EntretiensTable"></table>
<div id="EntretiensPager">
</div>
<div id="dialogDeleteEntretien" title="Supprimer un entretien" style="display: none;">
    <label style="width: 350px"> Voulez-vous vraiment supprimer cet entretien ?</label>
    <p style="float: right">
        @Html.ActionImage("", "", "~/Content/Images/Valider.png", "Valider", "32px", "32px", "DeleteEntretien()")
        @Html.ActionImage("Index", "", "~/Content/Images/Annuler.png", "Annuler", "32px", "32px", "")
    </p>
</div>
<div id="dialogAddEntretien" title="Ajouter un entretien" style="display: none;">

</div>

<div id="dialogEditEntretien" title="Edition d'un entretien" style="display: none;">

</div>